package com.media.photocamera.imagelist;
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.media.photocamera.R
import java.io.File

class PreviewImageAdapter(var changeCallBack:ChangeCallBack) : RecyclerView.Adapter<PreviewImageHolder>() {

    var data: ArrayList<CameraImageBean> = ArrayList<CameraImageBean>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreviewImageHolder {
        return PreviewImageHolder(LayoutInflater.from(parent.context).inflate(R.layout.camerax_rv_item_preview_localimage, null, false))
    }

    override fun onBindViewHolder(holder: PreviewImageHolder, position: Int) {
        data.let {
            Glide.with(holder.iv_target.context)
                .load(File(data.get(position).path))
                .into(holder.iv_target)
        }
        holder.iv_close.setOnClickListener {
            data.removeAt(position)
            notifyDataSetChanged()
            changeCallBack?.countChange(data.size)
        }
    }

    override fun getItemCount(): Int {
        if (data.isNullOrEmpty()) {
            return 0
        }
        return data.size
    }

    fun addItem(iamgeBean: CameraImageBean) {
        data.add(iamgeBean)
        notifyDataSetChanged()
        changeCallBack?.countChange(data.size)

    }


    interface ChangeCallBack{
        fun countChange(count:Int)
    }

}